<template>
  <div class="createPost-container" title="Bookmark">
    <el-form ref="dataForm" :rules="rules" :model="form" label-position="right" label-width="100px" class="form-container">
      <el-form-item label="Category" prop="category">
        <DictGroupInput ref="dictGroupInput" :value.sync="form.category" dict-group="bookmark_category" class="filter-item" placeholder="Category" />
      </el-form-item>
      <el-form-item label="Title" prop="title">
        <el-input v-model="form.title" clearable maxlength="150" show-word-limit />
      </el-form-item>
      <el-form-item label="Subtitle" prop="subtitle">
        <el-input v-model="form.subtitle" clearable maxlength="255" show-word-limit />
      </el-form-item>
      <el-form-item label="Label" prop="label">
        <el-input v-model="form.label" placeholder="Separated by commas" clearable maxlength="255" show-word-limit />
      </el-form-item>
      <el-form-item label="Url" prop="url">
        <el-input v-model="form.url" clearable maxlength="255" show-word-limit />
      </el-form-item>
      <el-form-item label="Description" prop="description">
        <el-input v-model="form.description" :autosize="{ minRows: 3, maxRows: 5 }" type="textarea" placeholder="description" maxlength="1024" show-word-limit />
      </el-form-item>
      <el-form-item>
        <el-button v-waves type="success" @click="submitForm">Save</el-button>
        <el-button v-show="!isEdit" v-waves @click="reset">Reset</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import DictGroupInput from '@/views/components/DictGroupInput'
import { createOrUpdateBookmark } from '@/api/info/bookmark'
import waves from '@/directive/waves' // waves directive

const defaultForm = {
  title: '',
  subtitle: '',
  category: '',
  label: '',
  url: '',
  description: '',
  uid: 0
}
export default {
  name: 'Bookmark',
  components: { DictGroupInput },
  directives: { waves },
  props: {
    isEdit: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      form: {},
      rules: {
        title: [{ required: true, message: 'title is required', trigger: 'blur' }],
        url: [{ required: true, message: 'url is required', trigger: 'blur' }],
        category: [{ required: true, message: 'category is required', trigger: 'change' }]
      },
      loading: false
    }
  },
  computed: {
    ...mapGetters(['uid'])
  },
  created() {
    console.log('created')
    if (this.isEdit) {
      const row = this.$route.params
      this.form = Object.assign({}, row, { category: row.category.toString() }, { createAt: undefined, modifyAt: undefined })
    } else {
      this.form = Object.assign({}, defaultForm, { uid: this.uid })
    }
  },
  methods: {
    submitForm() {
      console.log(this.form)
      this.$refs.dataForm.validate(valid => {
        if (valid) {
          this.loading = true
          createOrUpdateBookmark(this.form)
            .then(response => {
              this.$notify({
                title: 'Success',
                message: 'save successfully',
                type: 'success',
                duration: 2000
              })
              this.$refs.dictGroupInput.$emit('newDict', this.form.category)
              if (!this.isEdit) {
                this.reset()
              }
              this.$refs.dictGroupInput.$emit('reset', this.form.category)
            })
            .catch(() => {
              this.loading = false
            })
          this.loading = false
        }
      })
    },
    reset() {
      this.$refs['dataForm'].resetFields()
    }
  }
}
</script>

<style lang="scss" scoped>
.createPost-container {
  padding: 20px;
  .filter-item {
    padding: 5px 5px 0 10px;
  }
}
</style>
